﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO.Ports;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AGVCallSystem
{
    /// <summary>
    /// ComSetting.xaml 的交互逻辑
    /// </summary>
    public partial class ComSettingWindow : Window
    {
        public ComSettingWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<IPAddress> IPList = new List<IPAddress>();
            string name = Dns.GetHostName();
            IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
            foreach (IPAddress ip in ipadrlist)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    IPList.Add(ip);
                }
            }
            cb_IPAddress.ItemsSource = IPList;
            tx_Port.Text = ConfigurationManager.AppSettings["Port"];
        }

        private void btn_COMSet_Click(object sender, RoutedEventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["IP"].Value = cb_IPAddress.Text;
            int index = cb_IPAddress.Text.LastIndexOf(".");
            string ipSegment = cb_IPAddress.Text.Substring(0, index + 1);
            config.AppSettings.Settings["IPSegment"].Value = ipSegment;
            config.AppSettings.Settings["Port"].Value = tx_Port.Text;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            MessageBox.Show("设置成功！");
            this.Close();
        }

        private void btn_Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
